package com.sd.repay.service.impl;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.service.ClientVersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ClientVersionServiceImpl implements ClientVersionService {

    private static Logger log = LoggerFactory.getLogger(ClientVersionServiceImpl.class);

    @Autowired
    private BaseDao baseDao;


    @Override
    @Transactional
    public int addClientVersionInfo(Map<String, String> clientVersionParams) throws Exception {

        String sql = " INSERT INTO t_client_version(id,os_type,client_version,is_new,desc,down_flag,client_url) " +
                " VALUES(?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(clientVersionParams.get("osType"));
        params.add(clientVersionParams.get("clientVersion"));
        params.add(clientVersionParams.get("isNew"));
        params.add(clientVersionParams.get("desc"));
        params.add(clientVersionParams.get("downFlag"));
        params.add(clientVersionParams.get("clientUrl"));
        params.add(new Date());
        return baseDao.execute(sql, params.toArray());
    }

    @Override
    public Map<String, Object> getClientVersion(String osType, String clientVersion) throws Exception {
        String sql = " SELECT * FROM t_client_version WHERE os_type = ? AND client_version = ? ";
        return baseDao.findFirst(sql, new Object[]{osType, clientVersion});
    }

    @Override
    public Map<String, Object> getMustClientVersion(String osType, long currIndex, String downFlag) throws Exception {
        String sql = " SELECT * FROM t_client_version WHERE os_type = ? and id>? AND down_flag = ? order by id desc limit 1 ";
        return baseDao.findFirst(sql, new Object[]{osType, currIndex, downFlag});
    }


}